<template>
  <div class="customers-leave-container">
    <!-- 说明标签 -->
    <div class="customers-leave-label">
      <div>
        推客等级规则：按照经验值计算等级，上月经验值到下月1日会折损50%，每月16日计算等级
      </div>
      <div>
        如：上月累计经验值10000，到本月1日会折损为5000，到16日计算的是5000+本月累计的经验值，进行升降级
      </div>
    </div>

    <!-- 操作 -->
    <el-row class="customers-leave-row">
      <el-col :span="14" style="text-align:left;padding-left:15px" class="customers-leave-row-col">
        <el-button type="primary" size="small" style="padding:8px 30px;" @click="add">新 增</el-button>
      </el-col>
      <el-col :span="10" style="text-align:right;padding-right:40px">
        <el-input v-model="searchName" placeholder="请输入名称" size="small" style="max-width:240px" @keydown.enter="search">
          <el-button slot="suffix" type="text" size="small" class="search-button" @click="search">
            <i class="el-icon-search"></i>
          </el-button>
        </el-input>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <div>
      <el-table :data="leaveList" style="width:100%" stripe>
        <el-table-column prop="name" label="等级名称" align="center"></el-table-column>
        <el-table-column prop="min_exp" label="等级经验" align="center"></el-table-column>
        <el-table-column prop="reward_ratio" label="奖励比例(%)" align="center"></el-table-column>
        <el-table-column prop="format_create_time" label="创建时间" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope" width="80px">
            <el-switch v-model="scope.row.status" active-color="#13ce66" active-value="1" inactive-value="0" @change="changeStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text" 
              @click="eidt(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button class="action-button" 
              @click="del(scope.row)" type="text">
              <svg-icon icon-class="shanchu"/>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <!-- 新增 编辑 -->
    <el-dialog :visible.sync="disAddLeave" width="30%" :title="isAdd ? '新增' : '修改'">
      <add-leave :isAdd="isAdd" :leaveData="leaveData" @success="disAddLeave = false; getLeaveList()" v-if="disAddLeave"/>
    </el-dialog>
  </div>
</template>

<script>
import { getTuanLeaveList, putTuanLeave, delTuanLeave } from '@/api/customers'
import addLeave from './components/addLeave'

export default {
  name: 'customersLeave',
  components: {
    addLeave
  },
  data() {
    return {
      page: 1,
      count: 1,

      leaveList: [],
      searchName: '',

      disAddLeave: false,
      isAdd: true,

      leaveData: {
        key: this.$store.state.app.activeApp.saa_key,
        type: 1,
        min_exp: "",
        name: "",
        reward_ratio: "",
        status: "1"
      }
    }
  },
  mounted() {
    this.getLeaveList();
  },
  methods: {
    /**
     * 获取团长等级列表
     */
    getLeaveList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        type: 1,
        page: this.page,
        limit: 10
      };
      getTuanLeaveList(params).then(res => {
        if(res.status === 200){
          this.leaveList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.leaveList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 修改状态
     */
    changeStatus(data) {
      const params = { key: this.$store.state.app.activeApp.saa_key, id: data.id, status: data.status };
      putTuanLeave(params).then(res => {
        if(res.status === 200){
          this.$message.success('修改成功！');
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 删除
     */
    del(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id
      }
      delTuanLeave(params).then(response => {
        if(response.status === 200){
          this.getLeaveList();
          this.$message.success('删除成功！');
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 新增
     */
    add() {
      this.leaveData = {
        key: this.$store.state.app.activeApp.saa_key,
        type: 2,
        min_exp: "",
        name: "",
        reward_ratio: "",
        status: "1"
      };
      this.disAddLeave = true;
      this.isAdd = true;
    },
    /**
     * 编辑
     */
    eidt(data) {
      this.leaveData = {
        key: this.$store.state.app.activeApp.saa_key,
        type: 2,
        min_exp: parseInt(data.min_exp),
        name: data.name,
        reward_ratio: parseFloat(data.reward_ratio),
        status: data.status,
        id: data.id
      };
      this.disAddLeave = true;
      this.isAdd = false;
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getLeaveList();
    },
    /**
     * 搜索
     */
    search() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        type: 1,
        page: 1,
        limit: 10,
        searchName: this.searchName
      };
      getTuanLeaveList(params).then(res => {
        if(res.status === 200){
          this.leaveList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.leaveList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.customers-leave-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.customers-leave-row{
  margin: 15px 0;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 95%;
    overflow: hidden;
    display: flex;
  }
}
.customers-leave-label{
  width: 99%;
  padding: 10px 0px 10px 50px;
  margin: 10px 0px 20px 0px;
  border-radius: 5px;
  background-color: #ecf5ff;
  border: 1px solid #b3d8ff;
  color: #409eff;
  letter-spacing: 1px;
  &>div{
    padding: 1px 0;
  }
}
</style>